package com.ym.web;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.ym.domain.Device;
import com.ym.dto.DeviceResDto;
import com.ym.service.IDeviceService;
import com.ym.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/device")//localhost:8080/api/device/add
public class DeviceController {
    @Autowired
    private IDeviceService ideviceService;

    @GetMapping("/getDeviceInfoById")//localhost:8080/api/device/getDeviceInfoById?id=
    @SentinelResource(value = "service-device-limiti",
            blockHandler = "degradationMethod",
            fallback = "fuseFallback")
    public JSONResult selectDeviceById(@RequestParam Long id){
        DeviceResDto resDto = ideviceService.queryDeviceById(id);
        return JSONResult.success(resDto);
    }

    @GetMapping("/getDeviceList/byBuildId")//localhost:8080/api/device/getDeviceInfoById?id=
    @SentinelResource(value = "service-device-limiti",
            blockHandler = "degradationMethod",
            fallback = "fuseFallback")
    public List<Device> getDeviceList(@RequestParam Long id,@RequestParam Long floorId){
        List<Device> deviceList = ideviceService.getDeviceList(id,floorId);
        return deviceList;
    }

    @GetMapping("/getDeviceListByType")
    @SentinelResource(value = "service-device-limiti",
            blockHandler = "degradationMethod",
            fallback = "fuseFallback")
    public List<Device> getDeviceListByType(@RequestParam Long type){
        List<Device> deviceList = ideviceService.getDeviceListByType(type);
        return deviceList;
    }


    @PostMapping("/add")//localhost:8080/api/device/add
    public JSONResult addDevice(@RequestBody Device device){
        return ideviceService.add(device);
    }

    public JSONResult degradationMethod(Long id, BlockException e){
        e.printStackTrace();
        return JSONResult.error("服务降级,本类降级方法");
    }

    public JSONResult fuseFallback(Long id, Throwable e){
        e.printStackTrace();
        return JSONResult.error("熔断降级,本类降级方法");
    }
}

/*
*
*  "timestamp": "2023-05-09T16:58:07.143+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "[405] during [GET] to [http://service-device/device/getDeviceListByType] [DeviceFeignClient#getDeviceListByDeviceType(Integer)]: [{\"timestamp\":\"2023-05-09T16:58:06.986+0000\",\"status\":405,\"error\":\"Method Not Allowed\",\"message\":\"Request method 'POST' not supported\",\"path\":\"/device/getDeviceListByType\"}]",
    "path": "/build/getDeviceListByDeviceType"*/